package com.xiaozhi.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;

import com.xiaozhi.common.context.UserTokenContextHolder;
import com.xiaozhi.common.web.AjaxResult;
import com.xiaozhi.entity.DevicePic;
import com.xiaozhi.entity.DeviceVideo;
import com.xiaozhi.service.DevicePicServer;
import com.xiaozhi.service.DeviceVideoServer;
import com.xiaozhi.service.SysDeviceService;
import com.xiaozhi.service.WxUserService;
import com.xiaozhi.utils.AudioUtils;
import com.xiaozhi.vo.UserTokenVO;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/wx/device")
public class WxDeviceConfigController {
    private final DevicePicServer devicePicServer;
    private final DeviceVideoServer deviceVideoServer;
    private final WxUserService wxUserService;

    @Autowired
    private SysDeviceService sysDeviceService;

    @GetMapping("/quarypic")
    public Mono<AjaxResult> quarypic(String deviceId) {
        try {
            DevicePic devicePic = devicePicServer.selectPicByDeviceId(deviceId);
            AjaxResult result = AjaxResult.success();
            result.put("data", devicePic);
            return Mono.just(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Mono.just(AjaxResult.error());
        }
    }
    @PostMapping("/addpic")
    public Mono<AjaxResult> addpic(DevicePic devicePic,ServerWebExchange exchange) {
        try {
            devicePic.setCreateTime(new Date());
            devicePic.setUpdateTime(new Date());
            String token = UserTokenContextHolder.GetUserTokenFromWebExchange(exchange);
            UserTokenVO user = wxUserService.getUserFromToken(token);
            String openId = user.getOpenId();
            devicePic.setOpenId(openId);
            devicePic.setConvertListenPic(AudioUtils.convertJpg(devicePic.getListenPicUrl()));
            devicePic.setConvertTalkPic(AudioUtils.convertJpg(devicePic.getTalkPicUrl()));

            DevicePic queryPic = devicePicServer.selectPicByDeviceId(devicePic.getDeviceId());
            if(ObjectUtils.isEmpty(queryPic))
            {
                //不存在，插入
                devicePicServer.add(devicePic);
            }
            else{
                //更新
                devicePicServer.update(devicePic);
            }
            AjaxResult result = AjaxResult.success();
            result.put("data", devicePic);
            return Mono.just(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Mono.just(AjaxResult.error());
        }
    }
    @GetMapping("/quaryvideo")
    public Mono<AjaxResult> quaryvideo(String deviceId) {
        try {
            DeviceVideo deviceVideo = deviceVideoServer.selectVideoByDeviceId(deviceId);
            AjaxResult result = AjaxResult.success();
            result.put("data", deviceVideo);
            return Mono.just(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Mono.just(AjaxResult.error());
        }
    }
    @PostMapping("/addvideo")
    public Mono<AjaxResult> addvideo(DeviceVideo deviceVideo,ServerWebExchange exchange) {
        try {
            deviceVideo.setCreateTime(new Date());
            deviceVideo.setUpdateTime(new Date());
            String token = UserTokenContextHolder.GetUserTokenFromWebExchange(exchange);
            UserTokenVO user = wxUserService.getUserFromToken(token);
            String openId = user.getOpenId();
            deviceVideo.setOpenId(openId);
            deviceVideo.setConvertListenVideo("http://1.92.66.174:8091/uploads/"+AudioUtils.convertJpg(deviceVideo.getListenVideoUrl()));
            deviceVideo.setConvertTalkVideo("http://1.92.66.174:8091/uploads/"+AudioUtils.convertJpg(deviceVideo.getTalkVideoUrl()));

            DevicePic queryPic = devicePicServer.selectPicByDeviceId(deviceVideo.getDeviceId());
            if(ObjectUtils.isEmpty(queryPic))
            {
                //不存在，插入
                deviceVideoServer.add(deviceVideo);
            }
            else{
                //更新
                deviceVideoServer.update(deviceVideo);
            }
            AjaxResult result = AjaxResult.success();
            result.put("data", deviceVideo);
            return Mono.just(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Mono.just(AjaxResult.error());
        }
    }

    @PostMapping("/update/talktype")
    public Mono<AjaxResult> addvideo(String deviceId,Integer talkType,ServerWebExchange exchange) {
        try {
            sysDeviceService.updateTalkType(deviceId,talkType);
            AjaxResult result = AjaxResult.success();
           // result.put("data", deviceVideo);
            return Mono.just(result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Mono.just(AjaxResult.error());
        }
    }
}